package response

import (
	"encoding/json"
	"gitee.com/sdumeeting/sfu-server/global"
)

var (
	Success          = NewResponse(0, "success")
	ParamErr         = NewResponse(1001, "parameter format error")
	MeetingNotExist  = NewResponse(3002, "meeting does not exist")
	UserNotInMeeting = NewResponse(3004, "user is not in the meeting")
	ScreenSharingOn  = NewResponse(4001, "the screen is being shared now")
	ScreenSharingOff = NewResponse(4002, "the screen is not shared now")
)

// Response 定义响应格式
type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func NewResponse(code int, message string) *Response {
	return &Response{
		Code:    code,
		Message: message,
	}
}

func (r *Response) WithData(data interface{}) {
	r.Data = data
}

func (r *Response) ToByte() []byte {
	if byt, err := json.Marshal(r); err != nil {
		global.Logger.Errorf("Fail to convert map to byte")
		return nil
	} else {
		return byt
	}
}
